// pages/aboutMe/haidaotuan/create.js
var QRcode = require('../../../utils/weapp-qrcode');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    createBg: '../../../images/logo.png',
    qrCodeBg: '../../../images/qrCode.png',
    editBg: '../../../images/editW.png',
    isMe: false,
    showModalStatus: false,
    qrUrl: ''
  },

  changeImg(item) {
    
  },

  clickEdit() {
    let query = JSON.stringify(this.data.detail)
    wx.navigateTo({
      url: `/pages/aboutMe/dachuantuan/create?query=${query}`
    })
  },

  clickMe() {
    let _this = this
    // let token = app.globalData.token
    let query = `${this.data.detail.id}`
    console.log(query)
    let params = query;  // 二维码参数 
    var imgData = QRcode.drawImg(params, {
      typeNumber: 4,          // 密度
      errorCorrectLevel: 'L', // 纠错等级
      size: 800,              // 白色边框
    })
    _this.setData({
      showModalStatus: true,
      qrUrl: imgData
    })
  },

  hideModal() {
    this.setData({
      showModalStatus: false
    })
  },

  changeTab(item) {
    
  },

  resetImg() {
    const _this = this
    wx.showModal({
      title: '解散',
      content: '确认解散大船团吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.getURL() + 'book/material/disbandBigShip',
            method: 'POST',
            data: {
              creator: app.globalData.userInfo.id,
              id: _this.data.detail.id
            },
            success(res) {
              console.log(res)
              wx.showToast({
                title: '解散成功',
                icon: 'none',
                duration: 2000
              })
              wx.navigateTo({
                url: `/pages/aboutMe/bigList/list`
              })
            },
            fail(err) {
              wx.showToast({
                title: '请求服务器失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          console.log('用户点击取消');
        }
      }
    }); 
  },

  exitImg() {
    const _this = this
    wx.showModal({
      title: '退出',
      content: '确认退出大船团吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.getURL() + 'book/material/exitBigShip',
            method: 'POST',
            data: {
              userId: app.globalData.userInfo.id,
              pirateShipId: _this.data.detail.id
            },
            success(res) {
              console.log(res)
              wx.showToast({
                title: '退出成功',
                icon: 'none',
                duration: 2000
              })
              wx.navigateTo({
                url: `/pages/aboutMe/bigList/list`
              })
            },
            fail(err) {
              wx.showToast({
                title: '请求服务器失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          console.log('用户点击取消');
        }
      }
    }); 
  },

  createImg() {
    const _this = this
    wx.scanCode({
      onlyFromCamera: true,
      scanType: 'qrCode',
      success(res) {
        console.log({
          pirateShipId: _this.data.detail.id,
          userId: res.result
        })
        wx.request({
          url: app.getURL() + 'book/material/inviteBigShip',
          method: 'POST',
          data: {
            pirateShipId: _this.data.detail.id,
            userId: res.result
          },
          success(res) {
            console.log(res)
            if (res.data.code === 500) {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: '加入成功',
                icon: 'none',
                duration: 2000
              })
              wx.navigateTo({
                url: `/pages/aboutMe/bigList/list`
              })
            }
          },
          fail(err) {
            wx.showToast({
              title: '请求服务器失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      },
      fail(err) {
        wx.showToast({
          title: '请求服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const _this = this
    console.log(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(app.globalData)
    console.log(getCurrentPages())
    let pages = getCurrentPages()
    let current = pages[pages.length - 1]
    let detail = JSON.parse(current.options.query)
    console.log(detail, app.globalData.userInfo.id)
    let isMe = detail.creator == app.globalData.userInfo.id ? true : false
    this.setData({
      detail: detail,
      isMe: isMe
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})